<?php
/**
 * Controller for actions (in admin area) of admin module
 *  
 * @version 2012-10-26
 * @package admin
 * @subpackage controller
 * @category Admin_Controller_Admin
 */

class Admin_Controller_Admin extends Controller
{
	/** 
	* Index
	* Default function that will be used upon module start-up.
    *
    * @version 2012-10-10
    * @param Request $req
    * @return view
    * @access public
   */
    public function processIndex($req)
    {
		return $this->processAdmins($req);
    }

	/**
	 * Statistics
	 * This function shows statistics page.
	 * This function forms a query to the database with the help of a block of filters and returns an object VIEW with data.
	 * 
	 * @version 2012-10-26
	 * @param Request $req
	 * @return View
    * @access public 
	 */	
	public function processAdmins($req) {
		// Here local values are declared 
		$view = $this->getView("admin_admins");
		$dbMembers = Db::getModel('member_accounts');

		m('admins');
		
		$url = new Url();
		$pager = new Pager($url,_cfg('global.pager'));
		$sorter = new Sorter($url);
		$sorterCfg = array(
			'uuid',
			'login_key',
			'email',
			'last_visit',
			'last_ip',
			'phone',
			'blocked'
        );
		
		// Here filter conditions are set up
        $filterCfg= array(
            'login_key' => 'like',
			'email'=>'like',
			'ip'=>'like',
			'blocked'=>"in",
			'last_visit' => 'date_range'
        );
		$filter = new Filter('admin_member', $filterCfg);
		
		$filter->process($req);
		$sorter->initUp($sorterCfg);
		$sort   = array($sorter->getOrder());
		$where = $filter->getCriteria();
		$view->setVar('fltInfo',$filter->getInfo());
		$rows = $dbMembers->getMemberList(array_merge(array("utype"=>"='admin'"),$where),$pager,$sort);
		$tpl_tr = array('uuid'=>'tpl_tr','login_key'=>'null','class'=>'hide','ip'=>'0.0.0.0','blocked'=>'1');
		array_unshift($rows, $tpl_tr);
		$view->setVar('rows',$rows);
		
		$view->setVar('srtInfo', $sorter->getInfo());
		$view->setVar('pagerInfo',$pager->getInfo());
		return $view;
	}

	 /**
	 * AjaxAddAdmin
	 * This function add member in the system.
	 * The function updates the data in the database.
	 * 
	 * @version 2013-01-18
	 * @param Request $req
	 * @return string
     * @access public 
	 */
	function processAjaxAddAdmin($req){
		$dbAccounts = Db::getModel('member_accounts');
		$data = $req->getArray('data');
		$rules = array();
		
		$fields['data[email]'] = array('title'=>'Email', 'def' => 'email');
		$fields['data[login]'] = array('title'=>'Login', 'def' => 'login');
		$fields['data[pass]'] = array('title'=>'Password', 'def' => 'pass');
		
		$val = new Validator($fields);
		if(!$val->isValid($req->getArgs())){
			$result['status'] = "error";
			foreach($val->getErrors() as $e)
				$result['msg'] .= $e."<br>";
		}
		else{
			if($data['uuid']){
				$ant_email = $dbAccounts->find($data['email'],'email');
				$ant_login = $dbAccounts->find($data['login'],'login_key');
				$flag = true;
				if($data['uuid'] != $ant_email['uuid'] and $ant_email){
					$result['msg'] .= _fmt("account.email.exists")."<br>";
					$flag = false;
				}
				if($data['uuid'] != $ant_login['uuid'] and $ant_login['login_key'] == $data['login']){
					$result['msg'] .= _fmt("account.login.exists");
					$flag = false;
				}
				if($flag){
					$cfg = array(
						"login_key"=>$data['login'],
						"email"=>$data['email']
					);
					if($data['pass'] != _cfg('input.hide.pass'))
						$cfg['password_key'] = _pass($data['pass']);
					$dbAccounts->update($cfg,$data['uuid']);
					$result['msg'] .= _fmt("ok");
					$account = $dbAccounts->find($data['uuid']);
					$result['login'] = $account['login_key'];
					$result['email'] = $account['email'];
					$result['uuid'] = $account['uuid'];
					$result['status'] = "ok_update";
				}else{
					$result['status'] = "error";
				}
			}else{
				$request = $dbAccounts->addAdmin($data['login'],$data['pass'],$data['email']);
				if(!$request['id']){
					$result['status'] = "error";
					if($request['cnt_email'] == 1)
						$result['msg'] .= _fmt("account.email.exists")."<br>";
					if($request['cnt_acnt'] == 1)
						$result['msg'] .= _fmt("account.login.exists");
				}else{
					$result['status'] = "ok";
					$result['uuid'] = $request['uuid'];
					$result['msg'] .= _fmt("ok");
					$result['login'] = $data['login'];
					$result['email'] = $data['email'];
				}
			}
		}
		echo json_encode($result);
		exit;
	}
}